// *******************************************************************************************************
// # お問い合わせ画面
//   ・賃料
//   ・間取り
// *******************************************************************************************************

(function(win, doc) {
	/* 賃料
	----------------------------------- */
	function selectTinryo() {
		var $down = $('select[name=tinryo1]'),
			$up   = $('select[name=tinryo2]');

		$down.on('change', function(e){
			check();
		});
		$up.on('change', function(e){
			check();
		});

		function check() {
			setTimeout(function(e){
				var _val1 = parseInt($down.val());
					_val2 = parseInt($up.val());
				if (_val1 > _val2) {
					$down.val(_val2);
					$up.val(_val1);
				};
			}, 1);
		};
	}

	/* 間取り
	----------------------------------- */
	function selectMadori() {
		var $target = $('#contentMain .select_blc.madori'),
			$type = $target.find('.list_btn_type li'),
			$down = $('select[name=madori1]'),
			$up   = $('select[name=madori2]'),
			$room = $('input[name=room_type]'),
			data = {
				single: ['ワンルーム','1K','1DK','1LDK','2K','2DK'],
				double: ['1LDK','2K','2DK','2LDK','3K','3DK','3LDK'],
				family: ['2LDK','3K','3DK','3LDK','4K','4DK','4LDK','5K以上']
			},
			id = $room.val();

			console.log($target.find('.list_btn_type li').length)

		$type.each(function(i){
			var _id = $(this).attr('class');
			$(this).on('click', function(e){
				id = _id;
				$room.val(id);
				changeType();
			});
		});
		$down.on('change', function(e){
			changeRoom();
		});
		$up.on('change', function(e){
			changeRoom();
		});
		function changeType(){
			if (!id) return;
			$type.each(function(i){
				if ($(this).attr('class') == id) {
					$(this).find('a').addClass('active');
				} else {
					$(this).find('a').removeClass('active');
				};
			});
			$down.val(data[id][0]);
			$up.val(data[id][data[id].length-1]);
		};
		function changeRoom(){
			setTimeout(function(e){
				var _val1 = $down.val(),
					_val2 = $up.val(),
					_chk1 = 0,
					_chk2 = 0;
				$down.find('option').each(function(i){
					if ($(this).val() == _val1) {
						_chk1 = i;
						return false;
					};
				});
				$up.find('option').each(function(i){
					if ($(this).val() == _val2) {
						_chk2 = i;
						return false;
					};
				});
				if (_chk1 > _chk2) {
					$down.val(_val2);
					$up.val(_val1);
				};
			}, 1);
		};
		changeType();
	}


	//検索ボタンクリック
	$(".mod_btn_all").click(function() {
	
		if (!canSearch)
		{	
			return;
		}
		
		var items=[];
		var typ = $('input[name="search_type"]').attr("value");
		var listtype = $('#listtype').val();
		items.push({name:'search_type',value:typ});
		items.push({name:'page',value:1});
		
		var checkbox = $('input[name=ekis]');
		var ekicount = 1;
		for(var i = 0; i < checkbox.length; i++){
			if (checkbox[i].checked)
				{
					var str = checkbox[i].value.split('.');
					
					for(var j = 0; j < str.length; j++){
						items.push({name:"target_" + ekicount,value:str[j]});	
						ekicount++;
					}

				}
		}
		

		//AllHidden
//		$('input[type=hidden]').each(function(index){
//			var elem = $(this);
//			var val = elem.attr("value");
//			if(elem.attr('name').indexOf('madori') == 0 && elem.prop('checked')){
//				val = 'on';
//			}
//			if(elem.attr("name") != ""){
//				items.push({name:elem.attr("id"), value:val});
//			}			
//		});
		

		//検索結果ページ
		submit('/sp/search/tsukintsugakujikansearch/','POST',items);
		
	});
	
	/* 実行
	----------------------------------- */
	$(function() {
		selectTinryo();
		selectMadori();
	});

})(this, document);